<?php
namespace App\Http\Resources\Trade\Index;

use App\Http\Resources\ResourceCollection;
use App\Services\Utils\TencentYun\ImageService;
use App\Models\Trade\Common\SetBannerModel;

class BannerListResource extends ResourceCollection
{

    /**
     * Transform the resource into an array.
     *
     * @param
     *            \Illuminate\Http\Request
     * @return array
     */
    public function toArray($request)
    {
        return $this->collection->map(function ($item, $key) {
            return self::makeDataDecode($item);
        })->toArray();
    }

    /**
     * 制作使用数据（一条）
     */
    public static function makeDataDecode($item)
    {
        $data = array();
        $data['id'] = $item['id'];
        $data['uri'] = $item['uri'];
        // 图片
        $data['pic'] = ImageService::makeDownloadUri($item['pic'], ImageService::BUCKET_TRADE);
        // 原生跳转转义uri
        if (SetBannerModel::TYPE_APP) {
            switch ($item['page_key']) {
                case SetBannerModel::APP_PAGE_SUPPLY_DETAIL:
                    $data['uri'] = route('supplyDetail', json_decode($item['page_param'], true));
                    break; // 出售详情页
                case SetBannerModel::APP_PAGE_DEMAND_DETAIL:
                    $data['uri'] = route('demandDetail', json_decode($item['page_param'], true));
                    break; // 求购详情页
                case SetBannerModel::APP_PAGE_SHOP_DETAIL:
                    $data['uri'] = route('shopSupplyList', json_decode($item['page_param'], true));
                    break; // 店铺详情页
            }
        }
        return $data;
    }
}
